//
//  AppDelegate.m
//  HTAppBeauty
//
//  Created by standychen on 14-11-17.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "AppDelegate.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "HTSysParam.h"
#import "HTFrameSysUtils.h"
#import "HTFrameNet.h"
#import "htvlogin.h"


@interface AppDelegate ()
@end

@implementation AppDelegate

@synthesize backVersion;
@synthesize deviceTokenID;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    // Required
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //categories
            [APService
             registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                 UIUserNotificationTypeSound |
                                                 UIUserNotificationTypeAlert)
             categories:nil];
        } else {
            //categories nil
            [APService
             registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                 UIRemoteNotificationTypeSound |
                                                 UIRemoteNotificationTypeAlert)
#else
             //categories nil
             categories:nil];
            [APService
             registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                 UIRemoteNotificationTypeSound |
                                                 UIRemoteNotificationTypeAlert)
#endif
             // Required
             categories:nil];
        }
    [APService setupWithOption:launchOptions];
    
    

    //设置本地数据库名
    [HTFrameSysUtils dbNameSet:@"htmandb"];
    //加载参数
    [HTSysParam loadParamFormLocalDB];
    
    //设置平台服务地址
    [HTFrameNet defaultServerUrlSet:@"http://www.icoix.com:8080"];//58.210.191.210:199  192.168.0.56
    
    NSDictionary *dict=@{ @"erpserver":@"http://www.icoix.com:8080",@"erpvirdir":@"pinhui"};
    
    [HTSysParam loadParamWithDefault:dict];
    
    //访问缓存的数据
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userpassword = [defaults objectForKey:@"password"];
    id badgeNum=[defaults objectForKey:@"badgeNum"];
    if(badgeNum==nil){
        application.applicationIconBadgeNumber=0;
    }
    else{
        application.applicationIconBadgeNumber=[badgeNum intValue];
    }
    
    NSString *storyName=@"";
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        storyName=@"HTMain_New";
    }
    else{
        storyName=@"HTMain_IPAD";
    }
    
    //找到当前的storyboard对象
    UIStoryboard *story = [UIStoryboard storyboardWithName:storyName bundle:nil];
    
    if (userpassword == nil) {//没有记录密码，说明要从登陆开始
        [HTSysParam autoLoginFlagSet:false];//人为登录
        //没有保存，跳转登录页面
        UIViewController *login = [story instantiateViewControllerWithIdentifier:@"htvlogin_new"];
        self.window.rootViewController = login;
    }
    else{//记录了密码，说明概要从主页面开始

        [self.window makeKeyAndVisible];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UIViewController *main = [story instantiateViewControllerWithIdentifier:@"mdiVC"];
        self.window.rootViewController = main;
    }
    
    
   
    
    
    
    return YES;
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    [APService registerDeviceToken:deviceToken];
    NSLog(@"My token is: %@", deviceToken);
    deviceTokenID=[[NSString alloc]initWithFormat:@"%@",deviceToken];
    

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}


- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    [APService handleRemoteNotification:userInfo];
    NSLog(@"收到通知！");
    
}


- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
                        (^)(UIBackgroundFetchResult))completionHandler {
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    NSLog(@"移除通知！");
    application.applicationIconBadgeNumber  +=1;
    

}



- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    
    if (application.applicationState == UIApplicationStateActive) {
        
        [APService showLocalNotificationAtFront:notification identifierKey:@"identifierKey"];
    }
    else{
        NSLog(@"这是后台！");
    }
    
}








- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

//- (void)applicationWillEnterForeground:(UIApplication *)application {
//    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}









@end
